package com.sweets.look.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;

import com.sweets.look.constant.Constants;

public class SwipeDismissTouchListener implements View.OnTouchListener {
    public interface DismissCallbacks {
        boolean canDismiss(Object arg1);

        void onClickLand();

        void onDismiss(View arg1, Object arg2);
    }

    private final long mAnimationTime;
    private final DismissCallbacks mCallbacks;
    private int mDownX;
    private int mDownY;
    private int mLandWidth;
    private final int mMaxFlingVelocity;
    private final int mMinFlingVelocity;
    private final int mSlop;
    private boolean mSwiping;
    private int mSwipingSlop;
    private final Object mToken;
    private int mTranslationX;
    private VelocityTracker mVelocityTracker;
    private final View mView;
    private int mViewWidth;

    public SwipeDismissTouchListener(View view, Object token, int landwidth, DismissCallbacks callbacks) {
        super();
        this.mViewWidth = 1;
        this.mLandWidth = 0;
        ViewConfiguration v0 = ViewConfiguration.get(view.getContext());
        this.mSlop = v0.getScaledTouchSlop();
        this.mMinFlingVelocity = v0.getScaledMinimumFlingVelocity() * 16;
        this.mMaxFlingVelocity = v0.getScaledMaximumFlingVelocity();
        this.mAnimationTime = ((long) view.getContext().getResources().getInteger(android.R.integer.config_shortAnimTime));//17694720
        this.mView = view;
        this.mLandWidth = landwidth;
        this.mToken = token;
        this.mCallbacks = callbacks;
    }


    @SuppressLint(value = {"NewApi"})
    public boolean onTouch(View view, MotionEvent motionEvent) {
        int v10_1;
        boolean v10;
        motionEvent.offsetLocation(((float) this.mTranslationX), 0f);
        if (motionEvent.getX() > (((float) (view.getWidth() - this.mLandWidth)))) {
            if (motionEvent.getAction() == 1) {
                this.mCallbacks.onClickLand();
            }

            v10 = true;
            return v10;
        }

        if (this.mViewWidth < 2) {
            this.mViewWidth = this.mView.getWidth();
        }

        switch (motionEvent.getActionMasked()) {
            case 0: {
                this.mDownX = ((int) motionEvent.getRawX());
                this.mDownY = ((int) motionEvent.getRawY());
                if (this.mCallbacks.canDismiss(this.mToken)) {
                    this.mVelocityTracker = VelocityTracker.obtain();
                    this.mVelocityTracker.addMovement(motionEvent);
                }

                return false;
            }
            case 1: {
                if (this.mVelocityTracker == null) {
                    break;
                }

                int v5 = (((int) motionEvent.getRawX())) - this.mDownX;
                this.mVelocityTracker.addMovement(motionEvent);
                this.mVelocityTracker.computeCurrentVelocity(1000);
                int v9 = ((int) this.mVelocityTracker.getXVelocity());
                int v2 = Math.abs(v9);
                int v3 = ((int) Math.abs(this.mVelocityTracker.getYVelocity()));
                int v7 = 0;
                int v8 = 0;
                if (Math.abs(v5) > this.mViewWidth / 2 && (this.mSwiping)) {
                    v7 = 1;
                    if (v5 > 0) {
                        v8 = 1;
                    } else {
                        v8 = 0;
                    }

                } else if (this.mMinFlingVelocity <= v2 && v2 <= this.mMaxFlingVelocity && v3 < v2 && v3 < v2 && (this
                        .mSwiping)) {
                    int v11 = v9 < 0 ? 1 : 0;
                    v10_1 = v5 < 0 ? 1 : 0;
                    v7 = v11 == v10_1 ? 1 : 0;
                    v8 = this.mVelocityTracker.getXVelocity() > 0f ? 1 : 0;
                }

                if (v7 != 0) {
                    if (Constants.API >= 12) {
                        ViewPropertyAnimator v11_1 = this.mView.animate();
                        float v10_2 = v8 != 0 ? ((float) this.mViewWidth) : ((float) (-this.mViewWidth));
                        v11_1.translationX(v10_2).alpha(0f).setDuration(this.mAnimationTime).setListener(new
                                                                                                                 AnimatorListenerAdapter() {
                                                                                                                     public void onAnimationEnd(Animator animation) {
                                                                                                                         SwipeDismissTouchListener.this.performDismiss();
                                                                                                                     }
                                                                                                                 });

                    } else {
                        this.performDismiss();

                    }
                } else if ((this.mSwiping) && Constants.API >= 12) {
                    this.mView.animate().translationX(0f).alpha(1f).setDuration(this.mAnimationTime).setListener(
                            null);
                }

                this.mVelocityTracker.recycle();
                this.mVelocityTracker = null;
                this.mTranslationX = 0;
                this.mDownX = 0;
                this.mDownY = 0;
                this.mSwiping = false;
                break;
            }
            case 2: {
                if (this.mVelocityTracker == null) {
                    break;
                }

                this.mVelocityTracker.addMovement(motionEvent);
                int v5 = (((int) motionEvent.getRawX())) - this.mDownX;
                int v6 = (((int) motionEvent.getRawY())) - this.mDownY;
                if (Math.abs(v5) > this.mSlop && Math.abs(v6) < Math.abs(v5) / 2) {
                    this.mSwiping = true;
                    v10_1 = v5 > 0 ? this.mSlop : -this.mSlop;
                    this.mSwipingSlop = v10_1;
                    this.mView.getParent().requestDisallowInterceptTouchEvent(true);
                    MotionEvent v4 = MotionEvent.obtain(motionEvent);
                    v4.setAction(motionEvent.getActionIndex() << 8 | 3);
                    this.mView.onTouchEvent(v4);
                    v4.recycle();
                }

                if (this.mSwiping) {
                    this.mTranslationX = v5;
                    this.mView.setTranslationX(((float) (v5 - this.mSwipingSlop)));
                    this.mView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * (((float) Math.abs(v5))) / (((float)
                            this.mViewWidth)))));
                    v10 = true;
                } else {

                    v10 = false;
                }

                return v10;
            }
            case 3: {
                if (this.mVelocityTracker == null) {
                    break;
                }

                if (Constants.API >= 12) {
                    this.mView.animate().translationX(0f).alpha(1f).setDuration(this.mAnimationTime).setListener(
                            null);
                }

                this.mVelocityTracker.recycle();
                this.mVelocityTracker = null;
                this.mTranslationX = 0;
                this.mDownX = 0;
                this.mDownY = 0;
                this.mSwiping = false;
                break;
            }
        }

        return false;
    }

    private void performDismiss() {
        final ViewGroup.LayoutParams lp = this.mView.getLayoutParams();
        final int originalHeight = this.mView.getHeight();
        ValueAnimator v0 = ValueAnimator.ofInt(new int[]{originalHeight, 1}).setDuration(this.mAnimationTime);
        v0.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                SwipeDismissTouchListener.this.mCallbacks.onDismiss(SwipeDismissTouchListener.this.mView,
                        SwipeDismissTouchListener.this.mToken);
                SwipeDismissTouchListener.this.mView.setAlpha(1f);
                SwipeDismissTouchListener.this.mView.setTranslationX(0f);
                lp.height = originalHeight;
                SwipeDismissTouchListener.this.mView.setLayoutParams(lp);
            }
        });
        v0.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                lp.height = ((Integer)valueAnimator.getAnimatedValue()).intValue();
                SwipeDismissTouchListener.this.mView.setLayoutParams(lp);
            }
        });
        v0.start();
    }
}

